home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.04 Apr 91 / Code Optimizing / Sources / Mxmpy.a < prev    next >
Encoding:
Text File  |  1990-04-28  |  6.2 KB  |  167 lines  |  [TEXT/MPS ]

  1.       PRINT OFF
  2.       INCLUDE 'Traps.a'
  3.       INCLUDE 'SANEMacs.a'
  4.       PRINT ON
  5. ;-------------------------------------------------
  6. Mxmpy       PROC        EXPORT
  7. ;- - - - - - - - - - - - - - - - - - - - - - - - -
  8. ;  Performs the matrix multiplication C = A * B.
  9. ;
  10. ;  Calling sequence (FORTRAN):
  11. ;      CALL MXMPY (A, B, C, L, M, N)
  12. ;
  13. ;  where
  14. ;      A is an array with L rows and M columns
  15. ;      B is an array with M rows and N columns
  16. ;      C is an array with L rows and N columns
  17. ;      L, M, and N are INTEGERs.
  18. ;
  19. ;  NOTE:  All arrays must be completely filled,
  20. ;  with no gaps.  Do not try to pass part of an
  21. ;  array unless it forms a contiguous block of
  22. ;  memory locations.
  23. ;
  24. ;  April 1990
  25. ;  Jon Bell, Dept. of Physics & Computer Science
  26. ;  Presbyterian College, Clinton SC 29325
  27. ;
  28. ;  Written for MPW Assembler, v3.0.
  29. ;- - - - - - - - - - - - - - - - - - - - - - - - -
  30. ;  Locations of arguments to the subroutine,
  31. ;  relative to the address stored in register A6.
  32. a       EQU     28          ; addr. of a
  33. b       EQU     24          ; addr. of b
  34. c       EQU     20          ; addr. of c
  35. l       EQU     16          ; addr. of # rows in a
  36. m       EQU     12          ; addr. of # cols in a
  37. n       EQU     8           ; addr. of # cols in b
  38. ;  Locations of local variables, relative to the
  39. ;  address stored in register A6.
  40. sum         EQU -10 ; accumulates an element of c
  41. term        EQU -20 ; terms for an element of c
  42. termCount   EQU -24 ; initial value of term index
  43. rowCount    EQU -28 ; initial value of col. index
  44. aColSize    EQU -32 ; # of bytes per column of a
  45. bColSize    EQU -36 ; # of bytes per column of b
  46. ;  Other constants.
  47. ParamSize   EQU  24 ; # of bytes of parameters
  48. LocalSize   EQU -36 ; # of bytes of local var's.
  49. ;  Register usage.
  50. aPtr        EQU  A2 ; pointer into a
  51. bPtr        EQU  A3 ; pointer into b
  52. cPtr        EQU  A4 ; pointer into c
  53. rowIndex    EQU  D3 ; row-loop index
  54. colIndex    EQU  D4 ; column-loop index
  55. termIndex   EQU  D5 ; term-loop index
  56. aRowBase    EQU  D6 ; start of current row in a
  57. bColBase    EQU  D7 ; start of current col. in b
  58. ;- - - - - - - - - - - - - - - - - - - - - - - - -
  59. ;  Set up the stack frame, and save registers on
  60. ;  the stack.
  61.       LINK        A6, #LocalSize
  62.       MOVEM.L     A2-A4/D3-D7, -(SP)
  63. ;  Calculate and save the length of one 
  64. ;  column of a.
  65.       MOVE.L      l(A6), A0
  66.       MOVE.L      (A0), D0      ; # of rows
  67.       MULU        #10, D0       ; bytes per column
  68.       MOVE.L      D0, aColSize(A6)
  69. ;  Calculate and save the length of one 
  70. ;  column of b.
  71.       MOVE.L      m(A6), A0
  72.       MOVE.L      (A0), D0      ; # of rows
  73.       MULU        #10, D0       ; bytes per column
  74.       MOVE.L      D0, bColSize(A6)
  75. ;  Save the initial value of the term index.
  76.       MOVE.L      m(A6), A0
  77.       MOVE.L      (A0), termCount(A6)
  78. ;  Save the initial value of the row index.
  79.       MOVE.L      l(A6), A0
  80.       MOVE.L      (A0), rowCount(A6)
  81. ;  Initialize the column index.
  82.       MOVE.L      n(A6), A0
  83.       MOVE.L      (A0), colIndex
  84. ;  Initialize the base address of the current
  85. ;  column in b to the start of b.
  86.       MOVE.L      b(A6), bColBase
  87. ;  Initialize pointer into c.
  88.       MOVE.L      c(A6), cPtr
  89. BeginColLoop      ;  Cycle over the columns of c.
  90.             SUB.L       #1, colIndex
  91.             BMI.S       EndColLoop
  92.       ;  Initialize the row index.
  93.             MOVE.L      rowCount(A6), rowIndex
  94.       ;  Initialize the base address of the
  95.       ;  current row in a to the start of a.
  96.             MOVE.L      a(A6), aRowBase
  97. BeginRowLoop      ; Cycle over the rows of c.
  98.                   SUB.L       #1, rowIndex
  99.                   BMI.S       EndRowLoop
  100.             ;  Initialize the a and b pointers 
  101.             ;  for the next sum of terms.
  102.                   MOVE.L      aRowBase, aPtr
  103.                   MOVE.L      bColBase, bPtr
  104.             ;  Initialize the sum.
  105.                   LEA         sum(A6), A0
  106.                   CLR.L       (A0)+
  107.                   CLR.L       (A0)+
  108.                   CLR.W       (A0)
  109.             ;  Initialize the term index.
  110.                   MOVE.L  termCount(A6), termIndex
  111. BeginTermLoop     ; Cycle over the terms 
  112.                   ; in the sum.
  113.                         SUB.L       #1, termIndex
  114.                         BMI.S       EndTermLoop
  115.                   ;  Push the source and
  116.                   ;  destination addresses on the
  117.                   ;  stack for the multiplication.
  118.                         MOVE.L      aPtr, -(SP)
  119.                         LEA         term(A6), A0
  120.                         MOVE.L      A0, -(SP)
  121.                   ;  Copy the current element of b
  122.                   ;  to the destination, and
  123.                   ;  advance to the next element
  124.                   ;  in the current column of b.
  125.                         MOVE.L      (bPtr)+, (A0)+
  126.                         MOVE.L      (bPtr)+, (A0)+
  127.                         MOVE.W      (bPtr)+, (A0)
  128.                   ;  Perform the multiplication.
  129.                         FMULX
  130.                   ;  Add the new term to the sum.
  131.                         PEA         term(A6)
  132.                         PEA         sum(A6)
  133.                         FADDX
  134.                   ;  Advance to the next element
  135.                   ;  in the current row of a.
  136.                         ADDA.L  aColSize(A6), aPtr
  137.                         BRA.S       BeginTermLoop
  138. EndTermLoop
  139.             ;  Move the sum into the current
  140.             ;  element of c, and advance to the
  141.             ;  next row in the current column of
  142.             ;  c.  (At the end of the current
  143.             ;  column, this will wrap around to
  144.             ;  the next column.)
  145.                   LEA         sum(A6), A0
  146.                   MOVE.L      (A0)+, (cPtr)+
  147.                   MOVE.L      (A0)+, (cPtr)+
  148.                   MOVE.W      (A0), (cPtr)+
  149.             ;  Advance to the next row of a.
  150.                   ADD.L       #10, aRowBase
  151.                   BRA.S       BeginRowLoop
  152. EndRowLoop
  153.       ;  Advance to the next column of b.
  154.             ADD.L       bColSize(A6), bColBase
  155.             BRA.S       BeginColLoop
  156. EndColLoop
  157. ;  All done.  Restore the saved registers, 
  158. ;  clean up the stack and return.
  159.       MOVEM.L     (SP)+, A2-A4/D3-D7
  160.       UNLK        A6
  161.       MOVE.L      (SP)+, A0
  162.       ADDA.L      #ParamSize, SP
  163.       JMP         (A0)
  164.       DC.B        'MXMPY   '  ; label for debugger
  165.       ENDPROC
  166.       END
  167.